<?php

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

namespace Home\Controller;

use Zend\Mvc\Controller\AbstractActionController;
use Zend\View\Model\ViewModel;
use Core\Entity;
use Core\Service;
use Doctrine\ORM\EntityManager;

class IndexController extends AbstractActionController {

    protected $entityManager;

    protected function setEntityManager(EntityManager $em) {
        $this->entityManager = $em;
        return $this;
    }

    /**
     * Returns the EntityManager
     *
     * Fetches the EntityManager from ServiceLocator if it has not been initiated
     * and then returns it    
     *
     * @access protected
     * @return EntityManager
     */
    protected function getEntityManager() {
        if (null === $this->entityManager) {
            $this->setEntityManager($this->getServiceLocator()->get('Doctrine\ORM\EntityManager'));
        }
        return $this->entityManager;
    }

    public function indexAction() {

//        $objectManager = $this
//                ->getServiceLocator()
//                ->get('Doctrine\ORM\EntityManager');
//
//        $user = new \Core\Entity\User();
//        $user->setFullName('Marco Pivetta');
//
//        $objectManager->persist($user);
//        $objectManager->flush();
//
//        die(var_dump($user->getId())); // yes, I'm lazy
        //$objectManager = $this->getDoctrine()->getManager();
//        
//        
//        $userService= new \Core\Service\UserService();
//        $repository = $this->getEntityManager()->getRepository('\Core\Entity\User');
//        $users = $repository->findAll();
        
        
//        $userService= new \Core\Service\UserService();
//        print_r($userService->findAll($this->getEntityManager()));


        $view = new ViewModel();
        return $view;
    }

}
